<?php
namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;
use App\Http\Requests;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Input;
use Auth,Hash;
class UserController extends Controller {

    public function index()
    {
        if(auth::user()->dengji == 2){
            $map['city'] = auth::user()->city;
        }
        if(auth::user()->dengji == 3){
            $map['area'] = auth::user()->area;
        }
        $map['is_enabled'] = '1';
        $res = DB::table('admin_users')->where($map)->paginate(10);
        $count = DB::table('admin_users')->where($map)->count();
        return view('admin.user.index', ['res' => $res,'count'=>$count]);
    }

    public function add(Request $request)
    {
        if($request->isMethod('post')){
            if(Input::has('name') && Input::has('password') && Input::has('email')){
                $data = Input::except('_token','password');
                $data['password'] = bcrypt(Input::get('password'));
                DB::table('admin_users')->insert($data);
                return redirect('admin/user/index');
            }
            echo "<script>alert('用户名、密码及姓名不能为空!');window.history.go(-1);</script>";
        }
        return view('admin/user/add');
    }

    public function edit($id,Request $request)
    {
        $obj = DB::table('admin_users');
        $userinfo = $obj->where('id',$id)->first();
        $userinfo->area_id = explode(",",$userinfo->area_id);
        if($request->isMethod('post')){
            if(Input::has('name')){
                $data = Input::except('_token','permissions');
                $data['updated_at'] = date('Y-m-d H:i:s',time());

                if($data['role_id'] == 18){
                    $data['dengji'] = '1';
                }
                if($data['role_id'] == 24){
                    $data['dengji'] = '2';
                }
                if($data['role_id'] == 25){
                    $data['dengji'] = '3';
                }
                if($data['role_id'] == 15){
                    $data['dengji'] = '4';
                }
                //如果更改了用户等级，就清空原有的管理区域
                if($data['role_id'] != $userinfo->role_id){
                    $data['city'] = null;
                    $data['area'] = null;
                    $data['org_id'] = null;
                    $data['area_id'] = null;
                }
                $obj->where('id',$id)->update($data);
                return redirect('admin/user/index');
            }
            echo "<script>alert('姓名不能为空!');window.history.go(-1);</script>";
        }
        //权限组
        $res = DB::table('roles')->get();
        //市
        $city = db::table('area')->where('dengji','=','1')->orderby('id')->get();//市
        return view('admin.user.edit', ['userinfo' => $userinfo,'res'=>$res,'city'=>$city]);
    }

    public function edit_pwd($id,Request $request)
    {
        if($id != auth::user()->id){
            echo "<script>alert('暂无权限!');window.history.go(-1);</script>";
            exit;
        }
        $userinfo = db::table('admin_users')->where('id',$id)->first();
        if($request->isMethod('post')){
            $oldpassword = $request->input('oldpassword');
            $newpassword = $request->input('newpassword');
            $newpassword2 = $request->input('newpassword2');
            if(!$oldpassword){
                echo "<script>alert('原密码不能为空!');window.history.go(-1);</script>";
                exit;
            }
            if(!$newpassword){
                echo "<script>alert('新密码不能为空!');window.history.go(-1);</script>";
                exit;
            }
            if(!$newpassword2){
                echo "<script>alert('确认密码不能为空!');window.history.go(-1);</script>";
                exit;
            }
            if(!preg_match("/^(?![0-9]+$)(?![a-zA-Z]+$)[0-9A-Za-z]{6,16}$/",$newpassword)){
                echo "<script>alert('新密码长度为6-16位，且同时包含字母与数字!');window.history.go(-1);</script>";
                exit;
            }
            if($newpassword != $newpassword2){
                echo "<script>alert('两次输入密码不同!');window.history.go(-1);</script>";
                exit;
            }

            $res = DB::table('admin_users')->where('id',$id)->select('password')->first();

            if(!Hash::check($oldpassword, $res->password)){
                echo "<script>alert('原密码错误!');window.history.go(-1);</script>";
                exit;
            }else{
                $update = array(
                    'password'  =>bcrypt($newpassword),
                );
                $result = DB::table('admin_users')->where('id',$id)->update($update);
            }

            if($result){
                return redirect('admin/index');
            }else{
                echo "<script>alert('修改失败!');window.history.go(-1);</script>";
                exit;
            }
        }
        return view('admin.user.edit_pwd', ['userinfo' => $userinfo]);
    }

    public function del($id)
    {
        $re = db::table('admin_users')->where('id',$id)->first();
        if ($re) {
            DB::table('admin_users')->where('id',$id)->delete();
            return redirect()->back();
        } else {
            echo "<script>alert('删除失败!');window.history.go(-1);</script>";
        }
    }

    public function edit_area($id,Request $request)
    {
        $obj = DB::table('admin_users');
        $userinfo = $obj->where('id',$id)->first();
        if($userinfo->role_id == 22){
            echo "<script>alert('企业用户不能设置管辖区域!');window.history.go(-1);</script>";
        }
        if(!$userinfo->dengji){
            echo "<script>alert('请先设置用户级别!');window.history.go(-1);</script>";
        }
        if($userinfo->dengji == 1){
            echo "<script>alert('总队无需设置管辖区域!');window.history.go(-1);</script>";
        }
        $userinfo->area_id = explode(",",$userinfo->area_id);
        if($request->isMethod('post')){
            $data = Input::except('_token','permissions');
            //修改权限
            if(Input::has('permissions')){
                $arr = Input::get('permissions');
                $permission_id = '';
                //市级用户
                if($userinfo->dengji == 2){
                    if(count($arr) > 1){
                        echo "<script>alert('只能选择一个市!');window.history.go(-1);</script>";
                        die;
                    }
                    foreach ($arr as $k) {
                        //获取市下面的区
                        $qu = db::table('area')->where('cid','=',$k)->pluck('id');
                        foreach ($qu as $k2){
                            //获取区下面的派出所
                            $pcs = db::table('area')->where('cid','=',$k2)->pluck('id');
                            foreach ($pcs as $k3){
                                $permission_id .= $k3 . ',';
                            }
                            $permission_id .= $k2 . ',';
                        }
                        $permission_id .= $k . ',';
                        $data['city'] = $k;
                    }
                    $data['area_id'] = substr($permission_id, 0, -1);
                }

                //区级用户
                if($userinfo->dengji == 3){
                    if(count($arr) > 1){
                        echo "<script>alert('只能选择一个区!');window.history.go(-1);</script>";
                        die;
                    }
                    foreach ($arr as $k) {
                        //获取区上面的市
                        $shi = db::table('area')->where('id','=',$k)->value('cid');
                        $permission_id .= $shi . ',';
                        //获取区下面的派出所
                        $pcs = db::table('area')->where('cid','=',$k)->pluck('id');
                        foreach ($pcs as $k3){
                            $permission_id .= $k3 . ',';
                        }
                        $permission_id .= $k . ',';
                        $data['city'] = $shi;
                        $data['area'] = $k;
                    }
                    $data['area_id'] = substr($permission_id, 0, -1);
                }

                //派出所级用户
                if($userinfo->dengji == 4){
                    if(count($arr) > 1){
                        echo "<script>alert('只能选择一个派出所!');window.history.go(-1);</script>";
                        die;
                    }
                    foreach ($arr as $k) {
                        //获取派出所上面的区
                        $qu = db::table('area')->where('id','=',$k)->value('cid');
                        $permission_id .= $qu . ',';
                        //获取区上面的市
                        $shi = db::table('area')->where('id','=',$qu)->value('cid');
                        $permission_id .= $shi . ',';
                        $permission_id .= $k . ',';
                        $data['city'] = $shi;
                        $data['area'] = $qu;
                        $data['org_id'] = $k;
                    }
                    $data['area_id'] = substr($permission_id, 0, -1);
                }
            }

            $data['updated_at'] = date('Y-m-d H:i:s',time());
            $obj->where('id',$id)->update($data);
            return redirect('admin/user/index');
        }
        return view('admin.user.edit_area', ['userinfo' => $userinfo]);
    }

    public function shenhe_qiye(){
        $map['is_enabled'] = '2';
        $res = DB::table('admin_users')->where($map)->paginate(10);
        $count = DB::table('admin_users')->where($map)->count();
        return view('admin.user.shenhe_qiye', ['res' => $res,'count'=>$count]);
    }

    public function tongguo($id){
        $data['is_enabled'] = '1';
        $data['role_id'] = '22';
        $re = db::table('admin_users')->where('id',$id)->update($data);
        if ($re) {
            return redirect()->back();
        } else {
            echo "<script>alert('操作失败!');window.history.go(-1);</script>";
        }
    }

}


